import { _decorator, assetManager, AssetManager, AudioClip, Component, Node, SpriteFrame } from 'cc';
import { resPath } from '../data/enums';
import { nodePool } from './nodePool';
const { ccclass, property } = _decorator;

@ccclass('loadRes')
export class loadRes extends Component {
    // 资源是否加载完成
    isRes: boolean = false;
    // 存储加载的所有资源
    allBundleRes: { [key: string]: AssetManager.Bundle } = {};
    // 储存音乐
    allMusic: { [key: string]: AudioClip } = {};
    //储存图片
    allSpriteFrame: { [key: string]: SpriteFrame } = {};

    /**
     * 单例
     */
    private static _ins: loadRes = null;
    public static get ins() {
        if (!this._ins) {
            this._ins = new loadRes();
        }
        return this._ins;
    }
    /**
     * 加载bundle中的资源
     */
    async loadBundle(name: string) {
        assetManager.loadBundle(name, (err: string, budle: AssetManager.Bundle) => {
            if (budle) {
                this.allBundleRes[1] = budle;
                this.isRes = true;
                console.log(budle);
            } else {
                console.log('资源加载失败' + err);
            }
        });
    }

    /**
     * 指定的资源包中加载不同类型的游戏资源，并将这些资源分配到相应的集合中进行管理和使用
     */
    async resLoad(res: { type: any; path: any }) {
        //新声明一个方法，方便自调用，用于判断需要的资源是否加载完成，没有的话重新调用
        const startLoad = () => {
            // isRes为true代表总资源包加载完成
            if (this.isRes) {
                try {
                    this.allBundleRes[1].loadDir(
                        res.path,
                        res.type,
                        (err: any, assets: any[]) => {
                            if (assets) {
                                let linShiAss = null;
                                switch (res) {
                                    // ui预制体资源
                                    case resPath.UIFab:
                                        for (let i = 0; i < assets.length; i++) {
                                            linShiAss = assets[i];
                                            const name = linShiAss.data.name as string;
                                            nodePool.ins.setPrefab(name, linShiAss);
                                            console.log("加载UI成功" + name);
                                        }
                                        break;
                                    // 角色资源
                                    case resPath.game:
                                        for (let i = 0; i < assets.length; i++) {
                                            linShiAss = assets[i];
                                            const name = linShiAss.data.name as string;
                                            nodePool.ins.setPrefab(name, linShiAss);
                                            console.log("加载角色成功" + name);
                                        }
                                        break;
                                    // 音乐资源
                                    case resPath.music:
                                        for (let i = 0; i < assets.length; i++) {
                                            linShiAss = assets[i];
                                            if (!this.allMusic[linShiAss.name]) {
                                                this.allMusic[linShiAss.name] = linShiAss;
                                                console.log("加载音乐成功");
                                            }
                                        }
                                        break;
                                    // 音乐资源
                                    case resPath.image:
                                        for (let i = 0; i < assets.length; i++) {
                                            linShiAss = assets[i];
                                            if (!this.allSpriteFrame[linShiAss.name]) {
                                                this.allSpriteFrame[linShiAss.name] = linShiAss;
                                                console.log("加载图片成功"+linShiAss.name);
                                            }
                                        }
                                        break;
                                }
                            } else {
                                console.log(err);
                            }
                        }
                    );
                } catch (err) {
                    console.log('分载资源问题：' + err);
                }
            } else {
                //1秒后调用
                this.scheduleOnce(() => {
                    startLoad();
                }, 1);
            }
        };
        //第一次执行
        startLoad();
    }

    /**
     * 获取音乐数据
     */
    public getClip(name: string) {
        return this.allMusic[name];
    }
    /**
     * 获取图片资源
     */
    public getSpriteFrame(name: string) {
        return this.allSpriteFrame[name];
    }
}


